#!/bin/sh
#(c) copyright 2007 Barry Kauler LGPL v2
#launched from ~/.xinitrc, so X is running.

# see also /etc/sudoers -- set delayedrun does not require password,
# probably a security hole, need to look at it again later.
[ "`whoami`" != "root" ] && exec sudo -A ${0} ${@}

# variables created at bootup by 'init' in initramfs...
. /etc/rc.d/PUPSTATE
. /etc/DISTRO_SPECS
# rerwin: use distro-specific delayedrun if available.
[ -x ${0}-${DISTRO_FILE_PREFIX} ] && exec ${0}-${DISTRO_FILE_PREFIX}

. /etc/rc.d/BOOTCONSTRAINED

sleep 1 # let the dust settle first.

# slow cpus need more delay....
DELAYFACTOR=0
CPUMHZ=`grep -m 1 -i '^cpu MHz' /proc/cpuinfo | tr -d ' ' | cut -f 2 -d ':' | cut -f 1 -d '.'` #my laptop: 933.000
if [ $CPUMHZ ];then
 [ $CPUMHZ -gt 100 ] && DELAYFACTOR=$((900 / $CPUMHZ))
else # raspi: 697.95  my laptop: 4789.47
 BOGOMIPS=`grep -m 1 -i '^bogomips' /proc/cpuinfo | tr -d ' ' | cut -f 2 -d ':' | cut -f 1 -d '.'`
 [ $BOGOMIPS ] && [ $BOGOMIPS -gt 200 ] && DELAYFACTOR=$((800 / $BOGOMIPS))
fi
[ $DELAYFACTOR -gt 0 ] && sleep $DELAYFACTOR

# cedar hack 120210 (JustGreg) #moved down 120226
[ -x /usr/sbin/zcedar.sh ] && /usr/sbin/zcedar.sh

# tweaks for a full install
if [ $PUPMODE -eq 2 ] ; then
 if [ ! -f /var/local/delayedrun_firstboot_fullinstall_flag ];then
  if [ ! -f /tmp/delayedrun_finished_flag ];then
    sleep 7 #insane but otherwise pupswap is there in the background forever (toshiba nb100)
    ONLY_IF_NO_SWAP=1 pupswap set_dir /
  fi
  touch /var/local/delayedrun_firstboot_fullinstall_flag
 fi
fi

# offer to popup a getting-started html page...
# change logic so get these when first boot full install...
if [ ! -f /var/local/delayedrun_firstboot_flag ];then
  if [ ! -f /tmp/delayedrun_firstboot_flag ];then
    # if quicksetup restarts X, reenter here jumps this...
    touch /tmp/delayedrun_firstboot_flag
    # quick setup window for country and language and X resolution...
    QUICKSETUP="quicksetup"
    # rerwin: skip quicksetup if /etc/personal_settings_popup_disabled file created by remasterpup2
    [ ! -f /etc/personal_settings_popup_disabled ] && ${QUICKSETUP}
  fi
  if [ -c /dev/mixer ];then #test for sound ready, see /etc/init.d/10alsa.
   aplay /usr/share/audio/2barks.au &
  fi
  welcome1stboot
  sleep 3 # problem as tray applets only got launched after welcome msg exited.
  touch /var/local/delayedrun_firstboot_flag
fi

# handle multiple sound cards...
if which Multiple-Sound-Card-Wizard &> /dev/null ; then #pet pkg
	if [ $PUPMODE -eq 5 ];then
		ncards=$(grep '^ [0-9]' /proc/asound/cards | grep -v 'pcsp' | wc -l)
		if [ $ncards -gt 1 ] ; then
			Multiple-Sound-Card-Wizard
		fi
	fi
else
	mscw start
fi

if [ ! -f /tmp/delayedrun_finished_flag ] ; then
	# sleep a bit on first run
	# sometimes retrovol fails to show up.. might need some time
	sleep 3
	# this is in .xinitrc, in artfulpup there seems a timing issue, it doesn't work
	# on startup, but adding the same line here fixes the issue
	setxkbmap -option keypad:pointerkeys
fi

if [ -d /root/Startup ];then
 for A in /root/Startup/* ; do
  $A 2>/dev/null & 
  sleep 0.2
 done
fi

touch /tmp/delayedrun_finished_flag

### END ###
